﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.Resources;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Globalization;
using System.ComponentModel;
using DevExpress.XtraTab;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using ProgNetComponentsDx.Base.Components;

namespace ProgNetComponentsDx.Language
{
    [Designer(typeof(ProgNetComponentsDx.Language.Design.Designers.LanguageProviderDesigner))]
    public class LanguageProvider : BaseExtenderProvider<LanguageSettings>
    {
        public LanguageProvider()
        {
            Init();
        }
        public LanguageProvider(IContainer container)
            : base(container)
        {
            Init();
        }
        private void Init()
        {
            ProgNetLanguage.LanguageChanged += new EventHandler(ProgNetLanguage_LanguageChanged);
        }
        protected override void Dispose(bool disposing)
        {
            if(disposing)
                ProgNetLanguage.LanguageChanged -= new EventHandler(ProgNetLanguage_LanguageChanged);
            base.Dispose(disposing);
        }

        void ProgNetLanguage_LanguageChanged(object sender, EventArgs e)
        {
            this.RefreshAll();
        }

        protected override void ProcessObject(object target, LanguageSettings settings)
        {
            if (settings != null)
            {
                if (target is RadioGroup )
                {
                    ProcessRadionGroupItems((target as RadioGroup).Properties.Items, settings);
                    return;
                }
                if(target is RepositoryItemRadioGroup)
                {
                    ProcessRadionGroupItems((target as RepositoryItemRadioGroup).Items, settings);
                    return;
                }
                if ((target is SimpleButton) && 
                    settings.SetTextFromDialogResult && 
                    (target as SimpleButton).DialogResult != DialogResult.None)
                {
                    (target as SimpleButton).Text = ProgNetLanguage.sGetEnumString((target as SimpleButton).DialogResult);
                }
                else if (settings.StringCode.IsNotNull())
                {
                    if (target is BaseView)
                        (target as BaseView).ViewCaption = settings.Prefix + ProgNetLanguage.sGetString(settings.StringCode) + settings.Surfix;
                    else if (target is Control)
                        (target as Control).Text = settings.Prefix + ProgNetLanguage.sGetString(settings.StringCode) + settings.Surfix;
                    else if (target is BarItem)
                        (target as BarItem).Caption = settings.Prefix + ProgNetLanguage.sGetString(settings.StringCode) + settings.Surfix;
                    else if (target is GridColumn)
                        (target as GridColumn).Caption = settings.Prefix + ProgNetLanguage.sGetString(settings.StringCode) + settings.Surfix;
                }
            }
        }

        private void ProcessRadionGroupItems(DevExpress.XtraEditors.Controls.RadioGroupItemCollection items, LanguageSettings settings)
        {
            if (settings.RadioItemLngSettings != null &&
                settings.RadioItemLngSettings.Count > 0)
            {
                foreach (RadioItemLngSettings s in settings.RadioItemLngSettings)
                {
                    foreach (RadioGroupItem item in items)
                    {
                        if (s.ItemValue.EqualsSafe(item.Value))
                        {
                            if (s.StringCode.IsNotNull())
                                item.Description = ProgNetLanguage.sGetString(s.StringCode);
                        }
                    }
                }
            }
        }
       
        protected override LanguageSettings CreateSettings(object target)
        {
            LanguageSettings s = new LanguageSettings(target, this);
            return s;
        }

        public override bool CanExtend(object extendee)
        {
            if (extendee is LabelControl ||
                extendee is CheckEdit ||
                extendee is GridColumn ||
                extendee is Form ||
                extendee is SimpleButton ||
                extendee is BarItem ||
                extendee is ContainerControl ||
                extendee is XtraScrollableControl ||
                extendee is BaseView ||
                extendee is RadioGroup ||
                extendee is RepositoryItemRadioGroup)
                return true;
            return false;
        }

        public void DefaultColumnsNames()
        {
            this.BeginInit();
            foreach (KeyValuePair<object, LanguageSettings> kvp in AllObjects)
            {
                if (kvp.Key is GridColumn)
                {
                    kvp.Value.StringCode = (kvp.Key as GridColumn).FieldName;
                    AddKeyToChanged(kvp.Key);
                }
            }
            this.EndInit();
        }
        public string GetEnumString(Enum value)
        {
            return ProgNetLanguage.sGetEnumString(value);
        }
        public string GetStringFromMgr(string mgr, string code, params object[] args)
        {
            return ProgNetLanguage.sGetStringFromMgr(mgr, code, args);
        }
        public string GetString(string code, params object[] args)
        {
            return ProgNetLanguage.sGetString(code, args);
        }
    }
}
